home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 737 / prlabel / main.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  18KB  |  555 lines

  1. /***************************************************************************
  2.  
  3.    Program:    PrLabel
  4.    File:       main.c
  5.    
  6.    Version:    V1.2
  7.    Date:       28.10.91
  8.    Function:   Main routine
  9.    
  10.    Copyright:  SciTech Software 1991
  11.    Author:     Andrew C. R. Martin
  12.    Address:    SciTech Software
  13.                23, Stag Leys,
  14.                Ashtead,
  15.                Surrey,
  16.                KT21 2TD.
  17.    Phone:      +44 (0372) 275775
  18.    EMail:      UUCP: cbmuk!cbmuka!scitec!amartin
  19.                JANET: andrew@uk.ac.ox.biop
  20.                
  21. ****************************************************************************
  22.  
  23.    This program is Shareware with a suggested donation of £5.00.
  24.    It may be freely copied and distributed for no more than a nominal charge 
  25.    (not more than £4.00 in the U.K.) providing this header is included.
  26.    
  27.    The code may be modified as required, but any modifications must be
  28.    documented so that the person responsible can be identified. If someone
  29.    else breaks this code, I don't want to be blamed for code that does not
  30.    work! The code may not be sold commercially without prior permission from
  31.    the author, although it may be given away free with commercial products,
  32.    providing it is made clear that this program is free and that the source
  33.    code is provided with the program.
  34.  
  35. ****************************************************************************
  36.  
  37.    Description:
  38.    ============
  39.    PrLabel is a program to print text for laser labels.
  40.  
  41.    It uses STSLib to create 3D-look gadgets (under AmigaDOS V1.3 and V2.0)
  42.    and menus which will resize with the system default font.
  43.    
  44.    STSLib is available from SciTech Software for £20.00. This includes a 
  45.    full printed manual (>50 A5 pages) and the latest version of STSLib on
  46.    disk with source code.
  47.  
  48. ****************************************************************************
  49.  
  50.    Usage:
  51.    ======
  52.    PrLabel [-27][-28][-38]
  53.  
  54.    From the CLI, PrLabel may be invoked with one of the flags shown to 
  55.    select the label layout. This defaults to 3x8.
  56.  
  57.    From the Workbench, the Tooltype field may be set to one of:
  58.    FORMAT=27
  59.    FORMAT=28
  60.    FORMAT=38
  61.    to get the same result.
  62.  
  63. ****************************************************************************
  64.  
  65.    Revision History:
  66.    =================
  67.    V1.1     17.01.92
  68.    Changed interface to use STSLib.
  69.    V1.2     14.09.92
  70.    Program now reads and write address files using a TOOLTYPE defined
  71.    separator
  72.    
  73.  
  74. ***************************************************************************/
  75. #define MAIN 1
  76.  
  77. #include "PrLabel.h"
  78.  
  79. extern struct WBStartup *WBenchMsg; /* This is so we can get the program name */
  80.  
  81. /**************************************************************************/
  82. /*>main(int argc, char **argv)
  83.    ---------------------------
  84.    Main routine opens libs, gets tooltypes or command line parameters,
  85.    allocates first label, then sits in IDCMP loop.
  86.    28.10.91 Original
  87.    14.09.92 Added new tooltype/parameter as separator for addresses.
  88.             Added documentation for routine.
  89. */
  90. main(int argc,
  91.      char **argv)
  92. {
  93.    /* Disk object structure and char pointer for parsing tool types */
  94.    struct DiskObject    *DiskObj = NULL;
  95.    char                 *valptr;
  96.  
  97.    /* Variables for IDCMP handling */
  98.    struct IntuiMessage  *message = NULL;
  99.    ULONG                Signals;
  100.    ULONG                MIClass;
  101.    USHORT               MICode;
  102.    APTR                 MIAddress;
  103.    
  104.  
  105.    /* Set default sheet type and separator   */
  106.    SetSheet38((APTR)1);
  107.    GlobSepChar = ',';
  108.  
  109.    OpenLibraries();
  110.  
  111.    if(argc == 0)  /* Started from Workbench  */
  112.    {
  113.       if(DiskObj = (struct DiskObject *)GetDiskObject(WBenchMsg->sm_ArgList->wa_Name))
  114.       {
  115.          if(MatchToolValue(FindToolType(DiskObj->do_ToolTypes,"FORMAT"),"38"))
  116.             SetSheet38((APTR)1);
  117.          if(MatchToolValue(FindToolType(DiskObj->do_ToolTypes,"FORMAT"),"28"))
  118.             SetSheet28((APTR)1);
  119.          if(MatchToolValue(FindToolType(DiskObj->do_ToolTypes,"FORMAT"),"27"))
  120.             SetSheet27((APTR)1);
  121.          if((valptr = FindToolType(DiskObj->do_ToolTypes,"SEPARATOR")) != NULL)
  122.             GlobSepChar = valptr[0];
  123.          FreeDiskObject(DiskObj);
  124.       }
  125.       else
  126.       {
  127.          Die("Unable to get disk object information.");
  128.       }  
  129.    }
  130.    else           /* Started from CLI        */
  131.    {
  132.       while(--argc)
  133.       {
  134.          argv++;
  135.          if(argv[0][0] == '?')
  136.          {
  137.             Usage();
  138.          }
  139.          else if(argv[0][0] == '-')
  140.          {
  141.             switch(argv[0][1])
  142.             {
  143.             case '3':
  144.                SetSheet38((APTR)1);
  145.                break;
  146.             case '2':
  147.                {
  148.                   switch(argv[0][2])
  149.                   {
  150.                   case '7':
  151.                      SetSheet27((APTR)1);
  152.                      break;
  153.                   case '8':
  154.                      SetSheet28((APTR)1);
  155.                      break;
  156.                   default:
  157.                      Usage();
  158.                   }
  159.                }
  160.                break;
  161.             case 's':
  162.             case 'S':
  163.                GlobSepChar = argv[0][2];
  164.                break;
  165.             default:
  166.                Usage();
  167.             }
  168.          }
  169.       }
  170.    }
  171.  
  172.    OpenDisplay();
  173.  
  174.    switch(SheetType)
  175.    {
  176.    case THREE_EIGHT:
  177.       Label38();
  178.       break;
  179.    case TWO_EIGHT:
  180.       Label28();
  181.       break;
  182.    case TWO_SEVEN:
  183.       Label27();
  184.       break;
  185.    default:
  186.       break;
  187.    }
  188.  
  189.    /* Allocate space for first item in address label list */
  190.    address = (ADDR *)malloc(sizeof(ADDR));
  191.    if(!address)
  192.    {
  193.       Die("Unable to allocate memory");
  194.    }
  195.    address->line[0][0] = '\0';
  196.    address->line[1][0] = '\0';
  197.    address->line[2][0] = '\0';
  198.    address->line[3][0] = '\0';
  199.    address->line[4][0] = '\0';
  200.    address->line[5][0] = '\0';
  201.    address->line[6][0] = '\0';
  202.    address->prev       = NULL;
  203.    address->next       = NULL;
  204.  
  205.    /* Initialise so we don't print any labels to start */
  206.    ClearPrint();
  207.  
  208.    /* Activate the first text line gadget */
  209.    GoLine1((APTR)1);
  210.    
  211.    /* Main program loop */
  212.    for(;;)
  213.    {
  214.       /* Wait for messages from window */
  215.       Signals = Wait(1<<Window->UserPort->mp_SigBit);
  216.       /* Handle messages from the window */
  217.       while (message = (struct IntuiMessage *)
  218.          GetMsg(Window->UserPort))
  219.       {
  220.          MIClass   = message->Class;
  221.          MICode    = message->Code;
  222.          MIAddress = message->IAddress;
  223.          
  224.  
  225.          ReplyMsg(message);
  226.  
  227.          switch (MIClass)
  228.          {
  229.             case MENUPICK:
  230.                if ((MICode) != MENUNULL)
  231.                {
  232.                   HandleMenu(MICode);
  233.                }
  234.                break;
  235.  
  236.             case GADGETDOWN:
  237.                break;
  238.  
  239.             case GADGETUP:
  240.                HandleEvent(MIAddress);
  241.                break;
  242.                
  243.             case CLOSEWINDOW:
  244.                QuitProgram((APTR)1);
  245.                break;
  246.  
  247.             default:
  248.                break;
  249.          }
  250.       }
  251.    }
  252.  
  253.    return(0);
  254. }
  255.  
  256.  
  257. /**************************************************************************/
  258. /*>void HandleEvent(APTR object)
  259.    -----------------------------
  260.    Dispatches events after menu or gadget activity.
  261.    28.10.91 Original
  262.    31.05.92 Added Read and Save
  263.    14.09.92 ANSI'd. Added documentation for routine.
  264. */
  265. void HandleEvent(APTR object)
  266. {
  267.   if (object == (APTR)MenuItem1) { ReadList(object); return; }
  268.   if (object == (APTR)MenuItem2) { SaveList(object); return; }
  269.   if (object == (APTR)MenuItem3) { About(object); return; }
  270.   if (object == (APTR)MenuItem4) { QuitProgram(object); return; }
  271.   if (object == (APTR)pos11) { SetPos11(object); return; }
  272.   if (object == (APTR)pos12) { SetPos12(object); return; }
  273.   if (object == (APTR)pos14) { SetPos14(object); return; }
  274.   if (object == (APTR)pos13) { SetPos13(object); return; }
  275.   if (object == (APTR)pos15) { SetPos15(object); return; }
  276.   if (object == (APTR)pos16) { SetPos16(object); return; }
  277.   if (object == (APTR)pos17) { SetPos17(object); return; }
  278.   if (object == (APTR)pos21) { SetPos21(object); return; }
  279.   if (object == (APTR)pos22) { SetPos22(object); return; }
  280.   if (object == (APTR)pos23) { SetPos23(object); return; }
  281.   if (object == (APTR)pos24) { SetPos24(object); return; }
  282.   if (object == (APTR)pos25) { SetPos25(object); return; }
  283.   if (object == (APTR)pos26) { SetPos26(object); return; }
  284.   if (object == (APTR)pos27) { SetPos27(object); return; }
  285.   if (object == (APTR)Line1) { GoLine2(object); return; }
  286.   if (object == (APTR)Line2) { GoLine3(object); return; }
  287.   if (object == (APTR)Line3) { GoLine4(object); return; }
  288.   if (object == (APTR)Line4) { GoLine5(object); return; }
  289.   if (object == (APTR)Line5) { GoLine6(object); return; }
  290.   if (object == (APTR)Line6) { GoLine7(object); return; }
  291.   if (object == (APTR)Line7) { GoLine1(object); return; }
  292.   if (object == (APTR)Print) { PrintLabels(object); return; }
  293.   if (object == (APTR)pos18) { SetPos18(object); return; }
  294.   if (object == (APTR)pos28) { SetPos28(object); return; }
  295.   if (object == (APTR)pos31) { SetPos31(object); return; }
  296.   if (object == (APTR)pos32) { SetPos32(object); return; }
  297.   if (object == (APTR)pos33) { SetPos33(object); return; }
  298.   if (object == (APTR)pos34) { SetPos34(object); return; }
  299.   if (object == (APTR)pos35) { SetPos35(object); return; }
  300.   if (object == (APTR)pos36) { SetPos36(object); return; }
  301.   if (object == (APTR)pos37) { SetPos37(object); return; }
  302.   if (object == (APTR)pos38) { SetPos38(object); return; }
  303.   if (object == (APTR)Prev)  { PrevLabel(object); return; }
  304.   if (object == (APTR)Next)  { NextLabel(object); return; }
  305.   if (object == (APTR)Kill)  { KillLabel(object); return; }
  306. }
  307.  
  308. /**************************************************************************/
  309. /*>HandleMenu(USHORT firstcode)
  310.    ----------------------------
  311.    Menu handler to deal with multi-selection
  312.    28.10.91 Original
  313.    14.01.92 Added documentation for routine.
  314. */
  315. HandleMenu(USHORT firstcode)
  316. {
  317.    struct MenuItem *item;
  318.    USHORT nextcode = firstcode;
  319.    while(nextcode != MENUNULL)
  320.    {
  321.       item = (struct MenuItem *)ItemAddress(Menu1, (long)nextcode);
  322.       HandleEvent((APTR)item);
  323.       nextcode = item->NextSelect;
  324.    }
  325.    return(0);
  326. }
  327. /**************************************************************************/
  328. /*>OpenLibraries(void)
  329.    -------------------
  330.    Opens all required libraries
  331.    28.10.91 Original
  332.    14.09.92 Added documentation for routine.
  333. */
  334. OpenLibraries(void)
  335. {
  336.    if ((IntuitionBase=(struct IntuitionBase *)
  337.                OpenLibrary("intuition.library",INTUITION_REV)) == NULL)
  338.                Die("Unable to open Intuition library");
  339.  
  340.    if ((GfxBase=(struct GfxBase *)
  341.                OpenLibrary("graphics.library",GRAPHICS_REV)) == NULL)
  342.                Die("Unable to open Graphics library");
  343.  
  344.    if ((IconBase=(struct Library *)
  345.                OpenLibrary("icon.library",0)) == NULL)
  346.                Die("Unable to open Icon library");
  347.  
  348.    AslBase=(struct Library *)OpenLibrary("asl.library",ASL_REV);
  349.    /* We don't make any checks as AslBase isn't essential */
  350.  
  351.    return(0);
  352. }
  353.  
  354. /**************************************************************************/
  355. /*>OpenDisplay(void)
  356.    -----------------
  357.    Builds window, menus and gadgets. Opens window.
  358.    28.10.91 Original
  359.    17.01.92 Changed to use STSLib
  360.    14.09.92 Added documentation for routine.
  361. */
  362. OpenDisplay(void)
  363. {
  364.    struct NewWindow *nw;
  365.    struct Screen    WBscrn;
  366.    
  367.    nw = BuildWindow(NULL,65,0,390,200,"PrLabel V1.2  SciTech Software 1992",
  368.                     0,1);
  369.    /* Modify the IDCMP flags of the basic NewWindow structure */
  370.    nw->IDCMPFlags = IDCMP_GADGETUP | IDCMP_MENUPICK | IDCMP_CLOSEWINDOW;
  371.  
  372.    /* Find the size of the menu bar from the WorkBench */
  373.    GetScreenData(&WBscrn,sizeof(struct Screen), WBENCHSCREEN, NULL);
  374.    mb  = WBscrn.WBorTop + WBscrn.Font->ta_YSize + 1;
  375.  
  376.    /* Create gadgets */
  377.    Line1 = BuildStringGadget(nw,Line1SIBuff,80,125,mb+5,  30,0,0,0,0);
  378.    Line2 = BuildStringGadget(nw,Line2SIBuff,80,125,mb+20, 30,0,0,0,0);
  379.    Line3 = BuildStringGadget(nw,Line3SIBuff,80,125,mb+35, 30,0,0,0,0);
  380.    Line4 = BuildStringGadget(nw,Line4SIBuff,80,125,mb+50, 30,0,0,0,0);
  381.    Line5 = BuildStringGadget(nw,Line5SIBuff,80,125,mb+65, 30,0,0,0,0);
  382.    Line6 = BuildStringGadget(nw,Line6SIBuff,80,125,mb+80, 30,0,0,0,0);
  383.    Line7 = BuildStringGadget(nw,Line7SIBuff,80,125,mb+95, 30,0,0,0,0);
  384.    
  385.    pos11 = BuildBoolGadget(nw," ",10,mb+5  ,1,1,0,0,0,1,0);
  386.    pos12 = BuildBoolGadget(nw," ",10,mb+19 ,1,1,0,0,0,1,0);
  387.    pos13 = BuildBoolGadget(nw," ",10,mb+33 ,1,1,0,0,0,1,0);
  388.    pos14 = BuildBoolGadget(nw," ",10,mb+47 ,1,1,0,0,0,1,0);
  389.    pos15 = BuildBoolGadget(nw," ",10,mb+61 ,1,1,0,0,0,1,0);
  390.    pos16 = BuildBoolGadget(nw," ",10,mb+75 ,1,1,0,0,0,1,0);
  391.    pos17 = BuildBoolGadget(nw," ",10,mb+89 ,1,1,0,0,0,1,0);
  392.    if(SheetType == TWO_EIGHT || SheetType == THREE_EIGHT)
  393.       pos18 = BuildBoolGadget(nw," ",10,mb+103,1,1,0,0,0,1,0);
  394.    
  395.    pos21 = BuildBoolGadget(nw," ",38,mb+5  ,1,1,0,0,0,1,0);
  396.    pos22 = BuildBoolGadget(nw," ",38,mb+19 ,1,1,0,0,0,1,0);
  397.    pos23 = BuildBoolGadget(nw," ",38,mb+33 ,1,1,0,0,0,1,0);
  398.    pos24 = BuildBoolGadget(nw," ",38,mb+47 ,1,1,0,0,0,1,0);
  399.    pos25 = BuildBoolGadget(nw," ",38,mb+61 ,1,1,0,0,0,1,0);
  400.    pos26 = BuildBoolGadget(nw," ",38,mb+75 ,1,1,0,0,0,1,0);
  401.    pos27 = BuildBoolGadget(nw," ",38,mb+89 ,1,1,0,0,0,1,0);
  402.    if(SheetType == TWO_EIGHT || SheetType == THREE_EIGHT)
  403.       pos28 = BuildBoolGadget(nw," ",38,mb+103,1,1,0,0,0,1,0);
  404.    
  405.    if(SheetType == THREE_EIGHT)
  406.    {
  407.       pos31 = BuildBoolGadget(nw," ",66,mb+5  ,1,1,0,0,0,1,0);
  408.       pos32 = BuildBoolGadget(nw," ",66,mb+19 ,1,1,0,0,0,1,0);
  409.       pos33 = BuildBoolGadget(nw," ",66,mb+33 ,1,1,0,0,0,1,0);
  410.       pos34 = BuildBoolGadget(nw," ",66,mb+47 ,1,1,0,0,0,1,0);
  411.       pos35 = BuildBoolGadget(nw," ",66,mb+61 ,1,1,0,0,0,1,0);
  412.       pos36 = BuildBoolGadget(nw," ",66,mb+75 ,1,1,0,0,0,1,0);
  413.       pos37 = BuildBoolGadget(nw," ",66,mb+89 ,1,1,0,0,0,1,0);
  414.       pos38 = BuildBoolGadget(nw," ",66,mb+103,1,1,0,0,0,1,0);
  415.    }
  416.    
  417.    Kill  = BuildBoolGadget(nw,"KILL",  230,mb+141,0,1,0,0,0,0,0);
  418.    Next  = BuildBoolGadget(nw,"NEXT",  255,mb+125,0,1,0,0,0,0,0);
  419.    Prev  = BuildBoolGadget(nw,"PREV",  202,mb+125,0,1,0,0,0,0,0);
  420.    Print = BuildBoolGadget(nw,"PRINT!",11, mb+133,8,3,0,0,0,0,0);
  421.    
  422.    BuildFrame(nw,195,mb+118,120,44,0,0,1);
  423.    
  424.    /* Build Menus */
  425.    Menu1     = BuildMenu(NULL,NULL,"Project");
  426.    MenuItem1 = BuildMenuItem(NULL,0,(APTR)Menu1,"Open", 'O', 0,0,0,1);
  427.    MenuItem2 = BuildMenuItem(NULL,0,(APTR)Menu1,"Save", 'S', 0,0,0,1);
  428.    MenuItem3 = BuildMenuItem(NULL,0,(APTR)Menu1,"About",'\0',0,0,0,1);
  429.    MenuItem4 = BuildMenuItem(NULL,0,(APTR)Menu1,"Quit", 'Q', 0,0,0,1);
  430.  
  431.    Window = (struct Window *)OpenWindow(nw);
  432.  
  433.    SetMenuStrip(Window, Menu1);
  434.  
  435.    return(0);
  436. }
  437.  
  438. /**************************************************************************/
  439. /*>Cleanup()
  440.    ---------
  441.    Routine required by STSLib
  442.    17.01.91 Original
  443.    14.09.92 Added documentation for routine.
  444. */
  445. Cleanup()
  446. {
  447.    QuitProgram((APTR)0);
  448.  
  449.    return(0);
  450. }
  451.  
  452. /**************************************************************************/
  453. /*>QuitProgram(APTR object)
  454.    ------------------------
  455.    Closes the window and performs any other cleanup before exit.
  456.    28.10.91 Original
  457.    14.09.92 Added code to free STSLib stuff (oops!). Added documentation 
  458.             for routine.
  459. */
  460. QuitProgram(APTR object)
  461. {
  462.    if(Window) CloseWindow(Window);
  463.  
  464.    FreeSTSMenus(Menu1);
  465.    FreeSTSGadgets(Line1);
  466.  
  467.    exit(0);
  468.    
  469.    return(0);
  470. }
  471.  
  472. /**************************************************************************/
  473. /*>ClearPrint(void)
  474.    ----------------
  475.    Sets all labels so that they are not printed.
  476.    28.10.91 Original
  477.    14.09.92 Added documentation for routine.
  478. */
  479. ClearPrint(void)
  480. {
  481.    int j,
  482.        k;
  483.        
  484.    for(j=0;j<3;j++)
  485.       for(k=0;k<8;k++)
  486.          label[j][k].print = 0;
  487.          
  488.    return(0);
  489. }
  490.  
  491. /**************************************************************************/
  492. /*>Usage(void)
  493.    -----------
  494.    Displays a usage message.
  495.    28.10.91 Original
  496.    14.09.92 Added documentation for routine.
  497. */
  498. Usage(void)
  499. {
  500.    OpenIO();
  501.    
  502.    printf("PrLabel V1.2 Copyright SciTech Software, 1992\n");
  503.    printf("This program is freely distributable shareware (fee £5.00) \
  504. providing all files\n");
  505.    printf("are unaltered and distributed together. Only a nominal charge \
  506. may be made for\n");
  507.    printf("distribution.\n");
  508.    printf("Usage: PrLabel [-27][-28][-38][-s<c>]\n");
  509.    printf("       Prints text for address labels, etc.\n");
  510.    printf("       Flags -27,-28,-38 select the label layout (default: 3x8)\n");
  511.    printf("       Flag  -s followed by a character for record delimiter \
  512. (default: ,)\n\n");
  513.  
  514.    exit(0);
  515.    return(0);
  516. }
  517.  
  518. /**************************************************************************/
  519. /*>OpenIO(void)
  520.    ------------
  521.    Opens the console for default C Level 2 I/O. This is normally done by
  522.    the Lattice _main() code, but we link with tinymain.o which does not
  523.    include this.
  524.    28.10.91 Original
  525.    14.09.92 Added documentation for routine.
  526. */
  527. #include <ios1.h>
  528. #include <fcntl.h>
  529. extern struct UFB _ufbs[];
  530. extern int _fmode,_iomode;
  531.  
  532. OpenIO(void)
  533. {
  534.    register int x;
  535.  
  536.         _ufbs[0].ufbfh = Input();
  537.         _ufbs[1].ufbfh = Output();
  538.         _ufbs[2].ufbfh = Open("*", MODE_OLDFILE);
  539.         x = UFB_NC;                     /* do not close CLI defaults    */
  540.  
  541.    _ufbs[0].ufbflg |= UFB_RA | O_RAW | x;
  542.    _ufbs[1].ufbflg |= UFB_WA | O_RAW | x;
  543.    _ufbs[2].ufbflg |= UFB_RA | UFB_WA | O_RAW;
  544.  
  545.    x = (_fmode) ? 0 : _IOXLAT;
  546.    stdin->_file = 0;
  547.    stdin->_flag = _IOREAD | x;   
  548.    stdout->_file = 1;
  549.    stdout->_flag = _IOWRT | x;
  550.    stderr->_file = 2;
  551.    stderr->_flag = _IORW | x;
  552.  
  553.    return(0);
  554. }
  555.